package com.zkx.mvp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

import com.zkx.mvp.view.BaseActivity1;
import com.zkx.mvp.view.Fragment.ChatFragment;
import com.zkx.mvp.view.Fragment.ContactFragment;
import com.zkx.mvp.view.Fragment.FindFragment;
import com.zkx.mvp.view.Fragment.MineFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity1 {

    @BindView(R.id.btn_tab_chat1)
    Button btnTabChat;
    @BindView(R.id.btn_tab_contact)
    Button btnTabContact;
    @BindView(R.id.btn_tab_find)
    Button btnTabFind;
    @BindView(R.id.btn_tab_mine)
    Button btnTabMine;
    private ChatFragment mChatFragment;
    private ContactFragment mContactFragment;
    private FindFragment mFindFragment;
    private MineFragment mMineFragment;
    private FragmentManager mFragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mFragmentManager = getSupportFragmentManager();
        btnTabChat.setSelected(true);
        mChatFragment = new ChatFragment();
        mFragmentManager.beginTransaction().add(R.id.fragment_container, mChatFragment).commit();
    }

    private void switchState(Button selectedButton) {
        btnTabChat.setSelected(false);
        btnTabContact.setSelected(false);
        btnTabFind.setSelected(false);
        btnTabMine.setSelected(false);
        selectedButton.setSelected(true);
    }

    private void switchFragment(Fragment newFragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (!newFragment.isAdded()) {
            transaction.add(R.id.fragment_container, newFragment);
        }
        if (mChatFragment != null && mChatFragment.isVisible()) {
            transaction.hide(mChatFragment);
        }
        if (mContactFragment != null && mContactFragment.isVisible()) {
            transaction.hide(mContactFragment);
        }
        if (mFindFragment != null && mFindFragment.isVisible()) {
            transaction.hide(mFindFragment);
        }
        if (mMineFragment != null && mMineFragment.isVisible()) {
            transaction.hide(mMineFragment);
        }
        transaction.show(newFragment);
        transaction.commit();
    }
    @OnClick({R.id.btn_tab_chat1, R.id.btn_tab_contact, R.id.btn_tab_find, R.id.btn_tab_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_tab_chat1:
                if (mChatFragment == null) {
                    mChatFragment = new ChatFragment();
                }
//                switchTitleBar(getString(R.string.text_tab_chat), false, true);
                switchFragment(mChatFragment);
                switchState(btnTabChat);
                break;
            case R.id.btn_tab_contact:
                if (mContactFragment == null) {
                    mContactFragment = new ContactFragment();
                }
//                switchTitleBar(getString(R.string.text_tab_contact), true, false);
                switchFragment(mContactFragment);
                switchState(btnTabContact);
                break;
            case R.id.btn_tab_find:
                if (mFindFragment == null) {
                    mFindFragment = new FindFragment();
                }
//                switchTitleBar(getString(R.string.text_tab_find), false, false);
                switchFragment(mFindFragment);
                switchState(btnTabFind);
                break;
            case R.id.btn_tab_mine:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                }
//                switchTitleBar(getString(R.string.text_tab_mine), false, false);
                switchFragment(mMineFragment);
                switchState(btnTabMine);
                break;
        }
    }
}
